package com.nativetalk.controller.app.three.randking;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.three.ranking.RankingService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ThreeRankingController {
    private static Logger logger = Logger.getLogger(ThreeRankingController.class);
    @Resource
    private RankingService rankingService;

    /**
     * @return RetInfo
     * @Purpose 获取周排行榜
     * @version 3.2
     * @time    2015-08-25
     * @author lizhun
     */
    @RequestMapping(value = "/app/8/token/week/ranking/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo detail( @RequestHeader(value = "token", required = false) String token ) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            retInfo = rankingService.findWeekRanking(tdNtMember.getMemb_id());

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 领取奖励页面数据
     * @version 3.2
     * @time    2015-08-26
     * @author lizhun
     */
    @RequestMapping(value = "/app/8/token/week/ranking/reward/data", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo data( @RequestHeader(value = "token", required = false) String token ) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            retInfo = rankingService.findReviceWeekRanking(tdNtMember.getMemb_id());

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 学生领取奖励
     * @version 3.2
     * @time    2015-08-26
     * @author lizhun
     */
    @RequestMapping(value = "/app/8/token/week/ranking/reward/receive", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo receive(
            @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            retInfo = rankingService.reviceWeekRanking(tdNtMember.getMemb_id());

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 获取教师周排行榜
     * @version 3.2
     * @time    2015-08-25
     * @author lizhun
     */
    @RequestMapping(value = "/app/8/teatoken/week/ranking/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teadetail(
            @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            retInfo = rankingService.findTeacherWeekRanking(tdNtTeacherResult.getTea_id());

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 教师领取奖励页面数据
     * @version 3.2
     * @time    2015-08-26
     * @author lizhun
     */
    @RequestMapping(value = "/app/8/teatoken/week/ranking/reward/data", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teadata(
            @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            retInfo = rankingService.findTeacherReviceWeekRanking(tdNtTeacherResult.getTea_id());

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 教师领取奖励
     * @version 3.2
     * @time    2015-08-26
     * @author lizhun
     */
    @RequestMapping(value = "/app/8/teatoken/week/ranking/reward/receive", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teareceive(
            @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            retInfo = rankingService.reviceTeacherWeekRanking(tdNtTeacherResult.getTea_id());

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}



